#include "calib.h"
#include "mos.h"
#include "STC8G_H_Delay.h"
#include "ops.h"
#include "printf_xxx.h"

#define DELAY(x) delay_ms(x)
#define VAR PWM_MOS

void logf_scan(uint16_t T_ms, const uint32_t n, const uint8_t m, const uint8_t M)
{
    __xdata uint16_t i=1;
    __bit isInc=0;
    while(T_ms>8){
        //PWM变化方向
        if(VAR<=m){
            isInc=1;
        }if(VAR>=M){
            isInc=0;
        }
        //改变PWM值
        if(isInc){ //PWM增大，功率减小
            VAR++;
        }else{     //PWM增小，功率减大
            VAR--;
        }
        //改变频率
        i--;
        if(i==0){
            //分母情况 常数：周期(t)~sqrt(剩余时间)，1次方：线性扫频，2次方：对数扫频
            i=n/((uint32_t)T_ms*T_ms);
            if(i==0){
                i=1;
            }
            /*printf_s("i=");
            printf_u(i);
            printf_c('\n');*/
            T_ms--;
        }
        //延时
        DELAY(T_ms);
    }
}

void linear_scan(const uint8_t stop, const uint16_t ms)
{
    while(VAR != stop){
        DELAY(ms);
        if(VAR < stop){
            VAR++;
        }else{
            VAR--;
        }
    }
}